﻿using System;
using System.Linq;
using GlobalStore.Core.StorageAccess;
using GlobalStore.DataAccess.Entities;
using GlobalStore.DataAccess.Transactioning;
using NHibernate.Linq;
using System.IO;

namespace GlobalStore.SandBox
{
    class Program
    {
        static void Main(string[] args)
        {
            AccessorDescriptor freewebServerDescriptor =
            new AccessorDescriptor(
                new Uri("ftp://ftp2.freeweb.hu"),
                "kissandras31",
                "a1cce600"
            );

            IStorageAccessor freewebServerAccessor = new FtpServerAccessor(freewebServerDescriptor);

            Filepart aDotTxt = new Filepart("a.txt");

            Stream downloadStream = null;
            StreamReader streamReader = null;
            try
            {
                downloadStream = freewebServerAccessor.CreateFilepartDownloadStream(aDotTxt);
                streamReader = new StreamReader(downloadStream);

                Console.WriteLine(streamReader.ReadToEnd());
            }
            finally
            {
                if (streamReader != null)
                    streamReader.Close();
                if (downloadStream != null)
                    downloadStream.Close();
            }
        }

        private static void GetAllUser()
        {
            using (new TransactionScopeFactory().CreateReadOnly())
            {

                NHibernateTransactionScope.Current.Session.Query<User>().ToList()
                    .ForEach(u => Console.WriteLine("{0} : {1} - ({2})", u.Name, u.Password, u.UserRole));
            }
        }

        private static void InsertTestUsers()
        {
            using (var scope = new TransactionScopeFactory().Create())
            {
                NHibernateTransactionScope.Current.Session.Save(new User { Name = "vari", Password = "v", UserRole = UserRole.User });
                NHibernateTransactionScope.Current.Session.Save(new User { Name = "admin", Password = "a", UserRole = UserRole.Administrator });

                scope.Complete();
            }
        }

        private static void ReCreateDataBase()
        {
            using (var scope = new TransactionScopeFactory().CreateDataBaseRecreator())
            {
                scope.Complete();
            }
        }
    }
}
